﻿/**
 * @file videocall.c
 *
 */

/*********************
*      INCLUDES
*********************/
#include "lv_watch.h"

#if USE_LV_WATCH_VIDEOCALL != 0
#include "video_call.h"
#include <stdio.h>
#include <stdlib.h>
/*********************
*      DEFINES
*********************/

/**********************
*      TYPEDEFS
**********************/

/**********************
*  STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
#if VIDEO_SIGNAL_CHECK
static lv_task_t * signal_check_task = NULL;
#endif
static lv_task_t * rat_check_task = NULL;

/**********************
*      Global Variable
**********************/
voip_phone_call_params_t voipcall_params;
lv_color_t g_cbuf[canvas_width * canvas_heigh + 8]  __attribute__((aligned(8)));
#ifdef USE_WATCH_LITE
void * g_trans_buf;
#endif

/**********************
*  STATIC PROTOTYPES
**********************/
static lv_obj_t * voip_phonebook_create(lv_obj_t * activity_obj);
static lv_obj_t * voip_phone_cont_create(lv_obj_t * par, lv_align_t cont_layout, lv_style_t * style, lv_coord_t width);
static lv_obj_t * voip_ring_off_content_create(lv_obj_t * par);
static void voip_phone_dialing(lv_obj_t * content);
static void voip_phone_ring(lv_obj_t * content);
static void voip_phone_connected(lv_obj_t * content);
static void voip_phone_left_slide_callback(lv_obj_t * obj);
static void voip_phone_down_slide_callback(lv_obj_t * obj);
static void voip_dialing_animation(lv_obj_t * par);
static void voip_ring_animation(lv_obj_t * par);
static void voip_connected_animation(lv_obj_t * obj);
static void voip_volume_btn_action(lv_obj_t * obj, lv_event_t e);
static void voip_slider_action(lv_obj_t * slider, lv_event_t e);
static void voip_ring_hook_action(lv_obj_t * btn, lv_event_t e);
static void voip_phone_ring_off_timeout_cb(void * para);
static void voip_phone_callreq_timeout_cb(void * para);
static void voip_phone_cleanup(void);
static void voip_phone_obj_hide_anim_end(lv_anim_t * anim);
static void voip_anim_fp_dialing_ring(lv_obj_t * obj, int32_t x);
static void voip_anim_fp_connected(lv_anim_t * anim);
static void voip_phone_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * voip_phone_get_activity_obj(void);
static lv_obj_t * voip_phone_get_obj(void);
static void voip_get_time_elaps(uint32_t last_time, char * str, int8_t size);
static voip_phonebook_contact_t * voip_phonebook_get_contact_info(char * number);
static char * voip_phonebook_get_phone_number(uint8_t id); /*[0, NV_WATCH_MAX_CONTACT_NUM - 1]*/
static void voip_phone_voice_call_req(char * number, MODEM_VIDEO_CALL_TYPE voip_call_type);
static lv_obj_t * voip_phone_create(lv_obj_t * activity_obj, voip_phone_ui_e phone_ui_id);
static lv_res_t voip_phone_ring_off_create(void);
static void voip_ring_off_action(lv_obj_t * btn, lv_event_t e);
static bool voip_check_mo_permission(void);
static void voip_set_volume_when_slider_act(void);
static lv_res_t voip_call_fail_ind(lv_obj_t * obj);
static void voip_call_fail_message_cleanup(lv_anim_t * anim);
static void voip_local_remote_switch_action(lv_obj_t * btn, lv_event_t e);
static void canvas_button_hidden_event_cb(lv_obj_t * obj, lv_event_t e);
static void voip_signal_check_start(void * ext);
static void voip_signal_check_stop(void);
static bool is_rat_gsm(void);
static void voip_rat_check_start(void);
static void voip_rat_check_stop(void);
void voip_audio_ctrl_callback(AUDIO_CTRL_PRIORITY priority);

#if VOIP_PHONE_TEST
static void voip_phone_connected_cb(void * para);
static void watch_voip_call_response_originating(int32_t index);
#endif

#if VOIP_PHONEBOOK_TEST != 0
static void voip_phonebook_write_nvm_for_test(void);
static void voip_phonebook_clear_nvm_for_test(void);
#endif

/**********************
 *   FUNCTIONS DECLARATION
 ***********************/

/**********************
 *   GLOBAL FUNCTIONS
 ***********************/
void videocallmain_create(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_watch_png_cache_all_free();
        voip_phonebook_create(NULL);
    }
}

static nv_watch_phonebook_t * voip_phonebook_read_nvm(void)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
    uint32_t length = sizeof(nv_watch_phonebook_t);

    nv_watch_phonebook_t * nvm = (nv_watch_phonebook_t *)lv_mem_alloc(length);

    if(length != UI_NV_Read_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)nvm)) {
        printf("read nvm error in voip_phonebook_read_nvm\n");
        lv_mem_free(nvm);
        return(NULL);
    }
    return(nvm);
}

static void voip_phonebook_get_img_src(uint8_t portrait_id, const void ** img_src, const void ** img_src_small)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
    switch(portrait_id) {
        case WATCH_PORTRAIT_ID_FATHER:
            if(img_src) {
                *img_src = ICON_FATHER;
            }
            if(img_src_small) {
                *img_src_small = ICON_FATHER_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_MOTHER:
            if(img_src) {
                *img_src = ICON_MOTHER;
            }
            if(img_src_small) {
                *img_src_small = ICON_MOTHER_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_PATERNAL_GRADFATHER:
            if(img_src) {
                *img_src = ICON_PATERNAL_GRADFATHER;
            }
            if(img_src_small) {
                *img_src_small = ICON_PATERNAL_GRADFATHER_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_PATERNAL_GRADMOTHER:
            if(img_src) {
                *img_src = ICON_PATERNAL_GRADMOTHER;
            }
            if(img_src_small) {
                *img_src_small = ICON_PATERNAL_GRADMOTHER_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_MATERNAL_GRADFATHER:
            if(img_src) {
                *img_src = ICON_MATERNAL_GRADFATHER;
            }
            if(img_src_small) {
                *img_src_small = ICON_MATERNAL_GRADFATHER_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_MATERNAL_GRADMOTHER:
            if(img_src) {
                *img_src = ICON_MATERNAL_GRADMOTHER;
            }
            if(img_src_small) {
                *img_src_small = ICON_MATERNAL_GRADMOTHER_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_UNCLE:
            if(img_src) {
                *img_src = ICON_UNCLE;
            }
            if(img_src_small) {
                *img_src_small = ICON_UNCLE_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_AUNT:
            if(img_src) {
                *img_src = ICON_AUNT;
            }
            if(img_src_small) {
                *img_src_small = ICON_AUNT_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_BROTHER:
        case WATCH_PORTRAIT_ID_BOY:
            if(img_src) {
                *img_src = ICON_BOY;
            }
            if(img_src_small) {
                *img_src_small = ICON_BOY_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_SISTER:
        case WATCH_PORTRAIT_ID_GIRL:
            if(img_src) {
                *img_src = ICON_GIRL;
            }
            if(img_src_small) {
                *img_src_small = ICON_GIRL_SMALL;
            }
            break;

        case WATCH_PORTRAIT_ID_FAMILY:
        case WATCH_PORTRAIT_ID_OTHERS:
            if(img_src) {
                *img_src = ICON_FAMILY;
            }
            if(img_src_small) {
                *img_src_small = ICON_FAMILY_SMALL;
            }
            break;

        default:
            printf("warning! no icon corresponding to portrait_id %d", portrait_id);
            break;
    } /* switch */
} /* voip_phonebook_get_img_src */

voip_phonebook_contact_t * voip_phonebook_get_contact_info(char * number)
{
    printf("%s: number %s\n", __FUNCTION__, number);
    if(NULL == number) {
        printf("error, number is NULL in voip_phonebook_get_contact_info\n");
        return(NULL);
    }

    nv_watch_phonebook_t   *  nv_contact_list = voip_phonebook_read_nvm();
    voip_phonebook_contact_t * contact         = NULL;

    for(uint8_t i = 0; i < NV_WATCH_MAX_CONTACT_NUM; i++) {
        if('\0' == nv_contact_list->info[i].number[0]) {
            break;
        }

        if(0 == strncmp(number, nv_contact_list->info[i].number, strlen(number))) {
            contact = (voip_phonebook_contact_t *)lv_mem_alloc(sizeof(voip_phonebook_contact_t));
            memcpy(contact->contact_name, nv_contact_list->info[i].name, VOIP_PHONEBOOK_CONTACTS_MAX_NAME_LEN);
            memcpy(contact->contact_number, number, strlen(number) + 1);
            contact->contact_type = nv_contact_list->info[i].contact_type;
            voip_phonebook_get_img_src(nv_contact_list->info[i].portrait_id, &contact->img_src, &contact->img_src_small);
            printf("%s: find contact %s %s\n", __FUNCTION__, contact->contact_name, contact->contact_number);
            break;
        }
    }

    lv_mem_free(number);
    lv_mem_free(nv_contact_list);

    return(contact);
}

char * voip_phonebook_get_phone_number(uint8_t id)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    if((NV_WATCH_MAX_CONTACT_NUM - 1) < id) {
        return(NULL);
    }

    uint32_t length = sizeof(nv_watch_phonebook_info_t);
    nv_watch_phonebook_info_t * info = (nv_watch_phonebook_info_t *)lv_mem_alloc(length);
    uint32_t offset = id * length;
    if(length != UI_NV_Read_Req(NV_SECTION_UI_PHONEBOOK, offset, length, (uint8_t *)info)) {
        printf("read nvm error in voip_phonebook_get_phone_number\n");
        lv_mem_free(info);
        return(NULL);
    }

    uint8_t name_length = strlen(info->number);
    char * number = NULL;
    if(0 != name_length) {
        number = (char *)lv_mem_alloc(name_length + 1);
        memcpy(number, info->number, (name_length + 1));
        printf("%s: number %s in voipphonebook %d\n", __FUNCTION__, number, id);
    }
    lv_mem_free(info);

    return(number);
}

/**********************
*   STATIC FUNCTIONS
**********************/
void voip_audio_ctrl_callback(AUDIO_CTRL_PRIORITY priority)
{
    printf("%s,priority is %d\n", __FUNCTION__,priority);
    if(AUDIO_CTRL_PRIORITY_2 > priority)
    {
        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);
        if(activity_obj == NULL) return;
        voip_activity_destroy_req();
        Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_2);
    }
}

static lv_voip_phonebook_obj_ext_t * voip_phonebook_get_ext(void)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOIP_PHONEBOOK);

    if(activity_obj == NULL) {
        return(NULL);
    }

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) {
        return(NULL);
    }

    if(watch_obj) {
        lv_voip_phonebook_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return(ext);
    }
    return(NULL);
}

static void voip_phonebook_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_watch_png_cache_all_free();

    if(1 == lv_obj_get_user_data(activity_obj).user_num) {
        lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();
        if(ext == NULL) {
            return;
        }
        _lv_ll_clear(&ext->contact_list);
    }

#if VOIP_PHONEBOOK_TEST != 0
    voip_phonebook_clear_nvm_for_test();
#endif
}

static int voip_aligned_width(int val, lv_coord_t move_width)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    bool negative = (val < 0) ? true : false;
    if(val < 0) {
        val = -val;
    }
    int i       = (val + move_width - 1) / move_width - 1;
    int new_val = i * move_width;

    new_val = (val - new_val > move_width / 2) ? (new_val + move_width) : new_val;
    new_val = negative ? -new_val : new_val;
    return(new_val);
}

static lv_res_t voip_page_scrl_signal_handler(lv_obj_t * page_scrl, lv_signal_t sign, void * param)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return(LV_RES_INV);
    }

    if((sign == LV_SIGNAL_DRAG_END) || (sign == LV_SIGNAL_RELEASED)) {
        lv_coord_t new_x         = lv_obj_get_x(page_scrl);
        lv_coord_t aligned_new_x = voip_aligned_width(new_x, ext->move_width);
        lv_obj_set_x(page_scrl, aligned_new_x);

        lv_bidi_dir_t dir = lv_obj_get_base_dir(page_scrl);

        /* update image id */
        ext->cur_img_id = (aligned_new_x < 0) ? -aligned_new_x / ext->move_width : aligned_new_x / ext->move_width;
        voip_phonebook_t * contact;
        if(LV_BIDI_DIR_RTL == dir) {
            contact = _lv_ll_get_tail(&ext->contact_list);
        } else {
            contact = _lv_ll_get_head(&ext->contact_list);
        }
        uint8_t i = 0;
        while(NULL != contact) {
            if(ext->cur_img_id == i) {
                ext->cur_contact = contact;
                break;
            }
            i++;
            if(LV_BIDI_DIR_RTL == dir) {
                contact = _lv_ll_get_prev(&ext->contact_list, contact);
            } else {
                contact = _lv_ll_get_next(&ext->contact_list, contact);
            }
        }
        if(NULL == contact) {
            printf("error, contact not found in voip_page_scrl_signal_handler\n");
            return(LV_RES_INV);
        }
        lv_label_set_text(ext->label_phone_contact, contact->name);
        if(0 < strlen(contact->number)) {
            uint32_t pos = LV_LABEL_POS_LAST;
            if(LV_BIDI_DIR_RTL == dir) {
                pos = 0;
            }
            if(0 < strlen(contact->name)) {
                lv_label_ins_text(ext->label_phone_contact, pos, "  ");
            }
            lv_label_ins_text(ext->label_phone_contact, pos, contact->number);
        }
    }

    return(LV_RES_OK);
} /* voip_page_scrl_signal_handler */

static lv_res_t voip_phonebook_page_scrl_signal(lv_obj_t * page_scrl, lv_signal_t sign, void * param)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_res_t res;

    /* Include the ancient signal function */
    res = ancestor_signal(page_scrl, sign, param);
    if(res != LV_RES_OK) {
        return(res);
    }

    if(sign == LV_SIGNAL_CLEANUP) {
        /*when cleanup, can not invoke lv_indev_get_act */
        return(LV_RES_OK);
    }

    res = voip_page_scrl_signal_handler(page_scrl, sign, param);
    if(res != LV_RES_OK) {
        return(res);
    }

    return(LV_RES_OK);
}

static lv_res_t voip_phonebook_element_signal_func(lv_obj_t * element, lv_signal_t sign, void * param)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_res_t res;

    /* if not click signs, invoke ancient handler and return */
    if(sign != LV_SIGNAL_PRESSING && sign != LV_SIGNAL_PRESSED && sign != LV_SIGNAL_RELEASED && sign != LV_SIGNAL_PRESS_LOST) {
        /* Include the ancient signal function */
        lv_signal_cb_t sign_func = (lv_signal_cb_t)(lv_obj_get_user_data(element).user_data);
        res = sign_func(element, sign, param);
        if(res != LV_RES_OK) {
            return(res);
        }
        return(LV_RES_OK);
    }

    lv_obj_t * obj = lv_obj_get_parent(element);
    while(obj) {
        if(lv_obj_get_signal_cb(obj) != lv_watch_obj_signal) {
            obj = lv_obj_get_parent(obj);
        } else {
            break;
        }
    }
    if(obj == NULL) {
        return(LV_RES_INV);
    }

    res = lv_watch_obj_signal(obj, sign, param);
    if(res != LV_RES_OK) {
        return(res);
    }

    lv_voip_phonebook_obj_ext_t * ext = lv_obj_get_ext_attr(obj);
    if(ext == NULL) {
        return(LV_RES_INV);
    }
    if(ext->page_scrl == NULL) {
        return(LV_RES_INV);
    }
    res = voip_page_scrl_signal_handler(ext->page_scrl, sign, param);
    if(res != LV_RES_OK) {
        return(res);
    }

    /* Include the ancient signal function */
    lv_signal_cb_t sign_func = (lv_signal_cb_t)(lv_obj_get_user_data(element).user_data);
    res = sign_func(element, sign, param);
    if(res != LV_RES_OK) {
        return(res);
    }

    return(LV_RES_OK);
} /* voip_phonebook_element_signal_func */

static void voip_phonebook_add_element(lv_obj_t * element)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_watch_set_free_ptr(element, lv_obj_get_signal_cb(element));
    lv_obj_set_signal_cb(element, voip_phonebook_element_signal_func);
    lv_obj_add_protect(element, LV_PROTECT_PRESS_LOST | LV_PROTECT_CLICK_FOCUS);
}

static lv_obj_t * voipcall_number_add(lv_obj_t * page, const void * img_src)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_obj_t * img = NULL;

    lv_obj_t * content = lv_cont_create(page, NULL);

    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_size(content,  120, lv_obj_get_height(page));
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    LV_ASSERT_MEM(content);
    if(content == NULL) {
        return(NULL);
    }
    /*must set click enable for these content, If not, no pressed/pressing signals out*/
    lv_obj_set_click(content, true);
    voip_phonebook_add_element(content);
    /*hor drag in page, must set glue*/
    lv_page_glue_obj(content, true);

    img = lv_img_create(content, NULL);
    lv_img_set_src(img, img_src);
    lv_obj_align(img, content, LV_ALIGN_IN_TOP_MID, 0, 20);

    return(content);
}

static lv_obj_t * voip_append_obj(lv_obj_t * page)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();
    lv_obj_t * voip_append_obj = lv_obj_create(page, NULL);

    lv_obj_set_size(voip_append_obj, ext->append_width, lv_obj_get_height(page));
    lv_obj_add_style(voip_append_obj, LV_OBJ_PART_MAIN, &lv_style_transp_tight);
    voip_phonebook_add_element(voip_append_obj);
    return(voip_append_obj);
}

static lv_obj_t * voip_phonebook_add(lv_obj_t * page)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();
    voip_phonebook_t * contact = _lv_ll_get_head(&ext->contact_list);
    const void * img_src;

    voip_phonebook_get_img_src(contact->portrait_id, &img_src, NULL);

    lv_img_header_t img_header;
    lv_img_decoder_get_info(img_src, &img_header);
    ext->append_width = (lv_obj_get_width(page) - img_header.w - 2 * ext->body_padding_inner) / 2;
    voip_append_obj(page);

    while(NULL != contact) {
        voip_phonebook_get_img_src(contact->portrait_id, &img_src, NULL);
        voipcall_number_add(page, (void *)img_src);
        contact = _lv_ll_get_next(&ext->contact_list, contact);
    }

    voip_append_obj(page);

    return(page);
}

static void voip_phonebook_voicecall_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();
    uint8_t length = strlen(ext->cur_contact->number) + 1;
    char * number = (char *)lv_mem_alloc(length);

    memcpy(number, ext->cur_contact->number, length);

    if(voip_check_mo_permission()) {
        voip_phone_voice_call_req(number, MODEM_VIDEO_CALL_TYPE_VOICE);
    } else {
        voip_call_fail_ind(lv_watch_get_activity_obj(ACT_ID_VOIP_PHONEBOOK));
    }
}

static void voip_phonebook_voideocall_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();
    uint8_t length = strlen(ext->cur_contact->number) + 1;
    char * number = (char *)lv_mem_alloc(length);
    memcpy(number, ext->cur_contact->number, length);

    if(voip_check_mo_permission()) {
        voip_phone_voice_call_req(number, MODEM_VIDEO_CALL_TYPE_VIDEO);
    } else {
        voip_call_fail_ind(lv_watch_get_activity_obj(ACT_ID_VOIP_PHONEBOOK));
    }
}

static void voip_phonebook_add_btn(lv_obj_t * obj, char * phone_name, char * phone_number, lv_event_cb_t voiceaction, lv_event_cb_t videoaction)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_obj_t * content = lv_cont_create(obj, NULL);
    LV_ASSERT_MEM(content);
    if(content == NULL) {
        return;
    }
    printf("voip_phonebook_add_btn width: %d, height: %d\n", lv_obj_get_width(content), lv_obj_get_height(content));
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_cont_set_fit2(content, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_BOTTOM);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, 40);

    lv_obj_t * voicebtn;
    voicebtn = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(voicebtn, LV_BTN_STATE_RELEASED, ICON_CALL_PNG);
    lv_imgbtn_set_src(voicebtn, LV_BTN_STATE_PRESSED, ICON_CALL_PNG);
    lv_obj_align(voicebtn, content, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
    lv_obj_set_event_cb(voicebtn, voiceaction);
    voip_phonebook_add_element(voicebtn);

    lv_obj_t * videobtn;
    videobtn = lv_imgbtn_create(content, voicebtn);
    lv_obj_align(videobtn, content, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
    lv_obj_set_event_cb(videobtn, videoaction);
    voip_phonebook_add_element(videobtn);

    lv_voip_phonebook_obj_ext_t * ext = voip_phonebook_get_ext();;
    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
    lv_obj_set_width(label, lv_obj_get_width(obj) - 20);
    lv_label_set_text(label, phone_name);
    if(0 < strlen(phone_number)) {
        uint32_t pos = LV_LABEL_POS_LAST;
        if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(label)) {
            lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
            pos = 0;
        }
        if(0 < strlen(phone_name)) {
            lv_label_ins_text(label, pos, "  ");
        }
        lv_label_ins_text(label, pos, phone_number);
    }
    lv_obj_align(label, obj, LV_ALIGN_IN_BOTTOM_MID, 0, -20);
    ext->label_phone_contact = label;
} /* voip_phonebook_add_btn */

static void phonebook_note_create(lv_obj_t * obj, uint16_t text_id)
{
    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);

    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label, lv_obj_get_width(obj) - 50);
    lv_label_set_text_id(label, text_id);
    lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, 0);
}

static void voip_phonebook_add_contact(lv_obj_t * obj)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phonebook_obj_ext_t * ext = lv_obj_get_ext_attr(obj);

    lv_obj_t * page = lv_page_create(obj, NULL);
    LV_ASSERT_MEM(page);
    if(page == NULL) {
        return;
    }
    lv_obj_set_size(page, lv_obj_get_width(obj), lv_obj_get_height(obj));
    lv_page_set_scrollbar_mode(page, LV_SCROLLBAR_MODE_OFF);

    lv_obj_add_style(page, LV_PAGE_PART_BG, &lv_style_transp_tight);
    lv_obj_add_style(page, LV_PAGE_PART_SCROLLABLE, &lv_style_transp_tight);
    lv_obj_add_style(page, LV_PAGE_PART_SCROLLBAR, &lv_style_transp_tight);
    lv_page_set_scrollable_fit(page, LV_FIT_TIGHT);

    ext->page_scrl = lv_page_get_scrl(page);
    ancestor_signal = lv_obj_get_signal_cb(ext->page_scrl);
    lv_obj_set_signal_cb(ext->page_scrl, voip_phonebook_page_scrl_signal);
    /*the lv_page disable use acceleration, so sinal LV_SIGNAL_DRAG_END is same as LV_SIGNAL_RELEASED */
    lv_obj_set_drag_throw(ext->page_scrl, false);

    lv_page_set_scrl_layout(page, LV_LAYOUT_ROW_MID);
    voip_phonebook_add(page);
    voip_phonebook_add_element(ext->page_scrl);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(ext->page_scrl)) {
        uint8_t child_num = 0;
        lv_obj_t * child = lv_obj_get_child_back(ext->page_scrl, NULL);
        while(child) {
            child_num++;
            child = lv_obj_get_child_back(ext->page_scrl, child);
        }
        if(1 < child_num) {
            ext->cur_img_id = child_num - 1;
        }
    }
    lv_obj_set_x(ext->page_scrl, ((-1) * ext->move_width * ext->cur_img_id));

    voip_phonebook_add_btn(obj, ext->cur_contact->name, ext->cur_contact->number, voip_phonebook_voicecall_btn_action, voip_phonebook_voideocall_btn_action);
}

static void voip_phonebook_contacts_create(lv_obj_t * obj, lv_ll_t * contact_list)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);

    /*when pressing, y is not updated. When press lost  */
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_VER_UP_HIDE);
    lv_watch_obj_ext_t * watch_obj_ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_obj_ext_t));
    if(watch_obj_ext == NULL) {
        return;
    }

    /*Allocate the object type specific extended data*/
    lv_voip_phonebook_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_voip_phonebook_obj_ext_t));
    /*ext realloc based on watch_obj_ext, do not memeset it*/
    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }
    ext->contact_list.n_size = contact_list->n_size;
    ext->contact_list.head   = contact_list->head;
    ext->contact_list.tail   = contact_list->tail;
    ext->body_padding_inner  = 1;
    ext->append_width        = 50;
    ext->cur_img_id          = 0;
    ext->cur_contact         = _lv_ll_get_head(&ext->contact_list);
    ext->label_phone_contact = NULL;
    ext->move_width          = ext->body_padding_inner + ext->append_width + (lv_obj_get_width(obj) / 2 - ext->append_width); // 130;

    voip_phonebook_add_contact(obj);
}

#if VOIP_PHONEBOOK_TEST != 0

static void voip_phonebook_write_nvm(nv_watch_phonebook_t * nvm)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    if(NULL == nvm) {
        return;
    }

    uint32_t length = sizeof(nv_watch_phonebook_t);
    if(length != UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)nvm)) {
        printf("write contacts to nvm error in voip_phonebook_write_nvm\n");
        return;
    }
}

static void voip_phonebook_write_nvm_for_test(void)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    /*clear nvm*/
    uint32_t length = sizeof(nv_watch_phonebook_t);

    nv_watch_phonebook_t * nvm_clear = (nv_watch_phonebook_t *)lv_mem_alloc(length);
    memset(nvm_clear, 0, length);
    voip_phonebook_write_nvm(nvm_clear);
    lv_mem_free(nvm_clear);

    nv_watch_phonebook_t nvm_list = {     {
            {"test1", "JU234", 0, WATCH_PORTRAIT_ID_MOTHER},
            {"test2", "JU456", 1, WATCH_PORTRAIT_ID_FATHER},
            {"test3", "JU678", 1, WATCH_PORTRAIT_ID_GIRL},
            //{"test4", "xu2", 1, WATCH_PORTRAIT_ID_BOY},
        }
    };
    length = sizeof(nvm_list);
    if(length != UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)(&nvm_list))) {
        printf("write nvm error in voip_phonebook_write_nvm_for_test\n");
    }
}

static void voip_phonebook_clear_nvm_for_test(void)
{
    printf("%s\n", __FUNCTION__);

    uint32_t len = sizeof(nv_watch_phonebook_t);
    nv_watch_phonebook_t * nvm_pb = (nv_watch_phonebook_t *)lv_mem_alloc(len);
    memset(nvm_pb, 0, len);
    len = UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, len, (uint8_t *)nvm_pb);
    lv_mem_free(nvm_pb);
}
#endif

static lv_obj_t * voip_phonebook_create(lv_obj_t * activity_obj)
{
    // printf("%s %d\n", __FUNCTION__, __LINE__);
#if VOIP_PHONEBOOK_TEST != 0
    voip_phonebook_write_nvm_for_test();
#endif
    nv_watch_phonebook_t * nvm_contact_list = voip_phonebook_read_nvm();

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_VOIP_PHONEBOOK;
        activity_ext.create = voip_phonebook_create;
        activity_ext.prepare_destory = voip_phonebook_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) {
            return(NULL);
        }
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) {
        return(NULL);
    }

    if(setting_is_flying_mode()) {
        phonebook_note_create(obj, WATCH_TEXT_ID_PLEASE_TURN_OFF_FLYING_MODE);
        lv_watch_set_free_num(activity_obj, 0);
    } else if('\0' == nvm_contact_list->info[0].number[0]) {
        /*no contacts or network*/
        phonebook_note_create(obj, WATCH_TEXT_ID_NO_CONTACTS_OR_NETWORK);
        lv_watch_set_free_num(activity_obj, 0);
    } else {
        lv_ll_t contact_list;
        _lv_ll_init(&contact_list, sizeof(voip_phonebook_t));
        voip_phonebook_t * contact;
        uint32_t contact_length = sizeof(nv_watch_phonebook_info_t);
        for(uint8_t i = 0; i < NV_WATCH_MAX_CONTACT_NUM; i++) {
            if('\0' == nvm_contact_list->info[i].number[0]) {
                break;
            }
            contact = _lv_ll_ins_tail(&contact_list);
            memcpy(contact, &nvm_contact_list->info[i], contact_length);
        }
        voip_phonebook_contacts_create(obj, &contact_list);
        lv_watch_set_free_num(activity_obj, 1);
    }
#if VOIP_PHONEBOOK_TEST != 0
    /* not free because in heap*/
#else
    lv_mem_free(nvm_contact_list);
#endif
    return(obj);
} /* voip_phonebook_create */

static lv_obj_t * voip_phone_create(lv_obj_t * activity_obj, voip_phone_ui_e phone_ui_id)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    if(VOIP_PHONE_UI_CONNECTED == phone_ui_id) {
        /* set speaker volume to nv when leaving activiry's slider */
        voip_set_volume_when_slider_act();
    }

    /* cleanup phone first */
    voip_phone_cleanup();

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId           = ACT_ID_VOIPPHONE;
        activity_ext.create          = NULL;
        activity_ext.prepare_destory = voip_phone_prepare_destory;
        activity_obj                 = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) {
            return(NULL);
        }

#ifdef USE_WATCH_LITE
        if(g_trans_buf == NULL) {
            lv_img_cache_invalidate_src(NULL);
            g_trans_buf = lv_mem_aligned_alloc(sizeof(g_cbuf), 32);
        }
#endif
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) {
        return(NULL);
    }
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_LEFT_OR_RIGHT_SLIDE_CALLBACK);
    lv_watch_obj_set_left_slide_cb(obj, voip_phone_left_slide_callback);

    lv_voip_phone_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_voip_phone_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return(NULL);
    }

    ext->phone_status_label       = NULL;
    ext->phone_animation_obj      = NULL;
    ext->phone_ui_t               = phone_ui_id;
    ext->local_remote_switch_flag = VOIP_REMOTE_SWITCH_NULL;
#if ENABLE_VIDEO_SCALE == 0
    ext->voiph263fmt = VOIP_H263_FMT_QCIF;
#else
    ext->voiph263fmt = VOIP_H263_FMT_OTHER;
#endif

    hal_rtc_t rtc_curr;            // current rtc time
    Hal_Rtc_Gettime(&rtc_curr);
    ext->connect_start_time = time_to_seconds(&rtc_curr);
    ext->connect_time = NULL;
    ext->ring_value_button = NULL;
    ext->view_switch_button = NULL;
    ext->ring_off_button = NULL;
    ext->canvas_button_hidden_flag = NOT_HIDDEN;

    /* content */
    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_set_size(content, lv_obj_get_width(obj), lv_obj_get_height(obj) - 20);
    lv_cont_set_layout(content, LV_LAYOUT_COLUMN_MID);
    LV_ASSERT_MEM(content);
    if(content == NULL) {
        return(NULL);
    }
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    /* must click false */
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, 0);

    if(ext->phone_ui_t == VOIP_PHONE_UI_DIALING) {
        voip_phone_dialing(content);
        voip_dialing_animation(ext->phone_status_label);
    } else if(ext->phone_ui_t == VOIP_PHONE_UI_INCOMING) {
        voip_phone_ring(content);
        voip_ring_animation(ext->phone_status_label);
    } else if(ext->phone_ui_t == PHONE_UI_CONNECTED) {
        lv_obj_set_size(content, lv_obj_get_width(obj), lv_obj_get_height(obj));
        lv_obj_realign(content);
        voip_phone_connected(content);
        voip_connected_animation(ext->phone_status_label);
    }

    return(obj);
} /* voip_phone_create */

lv_res_t voip_phone_ring_off_create()
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();
    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return(LV_RES_INV);
    }

    // stop callreq timer
    if(NULL != voipcall_params.callreq_timer_id) {
        Hal_Timer_Stop(voipcall_params.callreq_timer_id);
        voipcall_params.callreq_timer_id = NULL;
    }

    if(ext->phone_ui_t == VOIP_PHONE_UI_INCOMING || ext->phone_ui_t == VOIP_PHONE_UI_DIALING) {
        if(ext->phone_status_label) {
            lv_label_set_text(ext->phone_status_label, "");
        }

        voip_ring_off_content_create(voip_phone_get_obj());
    } else if(ext->phone_ui_t == PHONE_UI_CONNECTED) {
        voip_ring_off_content_create(voip_phone_get_obj());
    }

    if(ext->phone_animation_obj) {
        lv_obj_del(ext->phone_animation_obj);
        ext->phone_animation_obj = NULL;
    }

    /* set speaker volume to nv when leaving activiry's slider */
    voip_set_volume_when_slider_act();

    /* phone cleanup when anim end*/
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, voip_phone_get_obj());
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)voip_phone_obj_hide_anim_end);
    lv_anim_set_time(&a, VOIP_PHONE_RING_OFF_CONTINUE_TIME);
    lv_anim_start(&a);

    voip_signal_check_stop();
    voip_rat_check_stop();

    return(LV_RES_OK);
} /* voip_phone_ring_off_create */

void voip_ring_off_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    printf("%s:  %d, state=%d\n", __FUNCTION__, __LINE__, voipcall_params.state);

    if(false == voipcall_params.ring_off) {
        voipcall_params.ring_off = true;

        if(NULL == voipcall_params.ring_off_timer_id) {
            voipcall_params.ring_off_timer_id = Hal_Timer_Start(VOIP_RING_OFF_TIMER_LEN, voip_phone_ring_off_timeout_cb, NULL, 0);
        }
        // stop callreq timer
        if(NULL != voipcall_params.callreq_timer_id) {
            Hal_Timer_Stop(voipcall_params.callreq_timer_id);
            voipcall_params.callreq_timer_id = NULL;
        }
    }

    switch(voipcall_params.state) {
        case MMI_VOIP_CALL_STATE_NULL:
        case MMI_VOIP_CALL_STATE_INVALID:
            // do noting
            break;

        case MMI_VOIP_CALL_STATE_ACTIVE:
            voip_call_hangup_req(TRUE);
            break;

        case MMI_VOIP_CALL_STATE_INCOMING:
            Hal_Tone_Play_End();
            voip_call_reject_req(TRUE);
            break;

        default:
            voip_call_hangup_req(TRUE);
            break;
    } /* switch */

    voip_signal_check_stop();
    voip_rat_check_stop();

#if VOIP_PHONE_TEST
    watch_voip_call_response_no_carrier(voipcall_params.index, 0);
#endif
} /* voip_ring_off_action */

lv_voip_phone_obj_ext_t * voip_phone_get_ext(void)
{
    lv_voip_phone_obj_ext_t * ext = NULL;

    lv_obj_t        *        watch_obj = voip_phone_get_obj();

    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }

    return(ext);
}

void * voip_phone_get_video_buffer()
{
    return g_cbuf;
}

#ifdef USE_WATCH_LITE
void * voip_phone_get_video_trans_buffer(void)
{
    return(g_trans_buf);
}
#endif
/**********************
*   STATIC FUNCTIONS
**********************/
static lv_obj_t * voip_phone_get_activity_obj(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);

    return(activity_obj);
}

static lv_obj_t * voip_phone_get_obj(void)
{
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = voip_phone_get_activity_obj();

    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }

    return(watch_obj);
}

#if VOIP_PHONE_TEST

static void voip_phone_connected_cb(void * para)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    watch_voip_call_response_connect(voipcall_params.index);
}
#endif

static void voip_phone_left_slide_callback(lv_obj_t * obj)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
#if VOIP_PHONE_TEST
    if((MMI_VOIP_CALL_STATE_INCOMING == voipcall_params.state)) {   // MT call
        int32_t index = 1;

        watch_voip_call_response_originating(index);
        watch_voip_call_response_alerting(index);

        Hal_Timer_Start(60, voip_phone_connected_cb, NULL, 0);
    }

    else if(MMI_VOIP_CALL_STATE_DIALING == voipcall_params.state) {    // MO call
        int32_t index = 1;

        watch_voip_call_response_originating(index);
        watch_voip_call_response_alerting(index);

        Hal_Timer_Start(60, voip_phone_connected_cb, NULL, 0);
    } else
#endif
        if((MMI_VOIP_CALL_STATE_ACTIVE == voipcall_params.state)
                && (MODEM_VIDEO_CALL_TYPE_VIDEO == voipcall_params.voip_call_type)) {
            voip_local_remote_switch_action(NULL, LV_EVENT_CLICKED);
        }
}

static void voip_phone_down_slide_callback(lv_obj_t * obj)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    voip_volume_btn_action(NULL, LV_EVENT_CLICKED);
}

static lv_obj_t * voip_phone_cont_create(lv_obj_t * par, lv_align_t cont_layout, lv_style_t * style, lv_coord_t width)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_obj_t * content = lv_cont_create(par, NULL);

    if(width > 0) {
        lv_cont_set_fit2(content, LV_FIT_NONE, LV_FIT_TIGHT);
        lv_obj_set_width(content, width);
    } else {
        lv_cont_set_fit2(content, LV_FIT_TIGHT, LV_FIT_TIGHT);
    }
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, style);
    lv_cont_set_layout(content, cont_layout);
    lv_obj_set_click(content, false);
    return(content);
}

static void voip_slider_action(lv_obj_t * slider, lv_event_t e)
{
    if(e == LV_EVENT_VALUE_CHANGED)
    {
        printf("%s %d\n", __FUNCTION__, __LINE__);

        uint8_t v = lv_slider_get_value(slider);
        if(v < 1) {
            lv_slider_set_value(slider, 1, LV_ANIM_OFF);
            return;
        }
        // set speaker volume
        if(!Hal_Set_BT_Audio_Path((HAL_SPEAKER_GAIN)v))
            Hal_Call_Speaker(HAL_CALL_SPK_ON, (HAL_SPEAKER_GAIN)v);
        voipcall_params.speaker_volume = v;
    }
}

static void voip_volume_btn_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    printf("%s %d\n", __FUNCTION__, __LINE__);
    slider_create(NULL, SETTING_VOIP_SPEAKER, 40, voip_slider_action);
    voipcall_params.speaker_volume = phone_get_call_speaker_volume();
}

static void voip_phone_obj_hide_anim_end(lv_anim_t * anim)
{
    (void)anim;
    printf("%s %d\n", __FUNCTION__, __LINE__);

    voip_phone_cleanup();
}

static lv_obj_t * voip_ring_off_content_create(lv_obj_t * par)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    if(par == NULL) {
        par = lv_scr_act();
    }

    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();
    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return(NULL);
    }

    lv_obj_t * obj = lv_obj_create(par, NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) {
        return(NULL);
    }
    lv_obj_set_size(obj, lv_obj_get_width(par), lv_obj_get_height(par));
    lv_obj_add_style(obj, LV_OBJ_PART_MAIN, &lv_style_transp_tight);

    lv_obj_t * content = lv_cont_create(obj, NULL);
    LV_ASSERT_MEM(content);
    if(content == NULL) {
        return(NULL);
    }

    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_cont_opa1);
    lv_obj_set_size(content, 204, 44);
    if(ext->phone_ui_t == VOIP_PHONE_UI_INCOMING || ext->phone_ui_t == VOIP_PHONE_UI_DIALING) {
        lv_obj_align(content, obj, LV_ALIGN_IN_TOP_MID, 0, 10);
    } else {
        lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, -12);
    }
    lv_cont_set_layout(content, LV_LAYOUT_CENTER);
    lv_obj_set_click(content, false);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);

    if(is_rat_gsm()) {
        lv_label_set_text_id(label, WATCH_TEXT_ID_NETWORK_WRONG);
    } else {
        if(ext->phone_ui_t == PHONE_UI_CONNECTED) {
            char tmp[20] = "";
            char str[30] = "";
            voip_get_time_elaps(ext->connect_start_time, tmp, 20);
            snprintf(str, 30, "%s(%s)", (char *)lv_lang_get_text(WATCH_TEXT_ID_RING_OFF), tmp);
            lv_label_set_text(label, str);
        } else {
            lv_label_set_text_id(label, WATCH_TEXT_ID_RING_OFF);
        }
    }

    lv_obj_set_click(label, false);

    return(obj);
} /* voip_ring_off_content_create */

static void voip_phone_ring_off_timeout_cb(void * para)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    voipcall_params.ring_off_timer_id = NULL;
    voipcall_params.ring_off_timeout  = true;
    watch_voip_call_response_no_carrier(voipcall_params.index, 0);
}

static void voip_phone_callreq_timeout_cb(void * para)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    voipcall_params.callreq_timer_id = NULL;
    voip_call_hangup_req(TRUE);
}

static void voip_ring_hook_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    printf("%s %d\n", __FUNCTION__, __LINE__);

    if(btn) {
        lv_obj_set_click(btn, false);
    }

    Hal_Tone_Play_End();

    int8_t ret = voip_call_answer_req();
    if(ret < 0) {
        watch_voip_call_response_no_carrier(voipcall_params.index, 0);
    }
}

static void voip_phone_cleanup(void)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_obj_t * slider_activit_obj = lv_watch_get_activity_obj(ACT_ID_SLIDER);

    if(slider_activit_obj) {
        lv_obj_del(slider_activit_obj);
    }

    lv_obj_t * activity_obj = voip_phone_get_activity_obj();
    if(activity_obj) {
        lv_obj_del(activity_obj);
    }
}

static void voip_anim_fp_dialing_ring(lv_obj_t * obj, int32_t x)
{
    if(obj != NULL) {
        lv_obj_set_x(obj, x);
    }
}

static void voip_dialing_animation(lv_obj_t * par)
{
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }

    lv_obj_t * img = lv_img_create(voip_phone_get_obj(), NULL);
    lv_img_set_src(img, LV_SYMBOL_RIGHT);
    lv_obj_add_style(img, LV_OBJ_PART_MAIN, &lv_watch_image);
    lv_obj_set_style_local_text_font(img, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_y(img, lv_obj_get_y(par) + lv_obj_get_y(lv_obj_get_parent(par)));
    ext->phone_animation_obj = img;

    /*Create an animation to move the button continuously left to right*/
    lv_anim_t a;
    lv_anim_path_t path;
    lv_anim_value_t start = lv_obj_get_x(par) + lv_obj_get_width(par) + 6;
    lv_anim_value_t end = start + 30;

    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_linear);

    lv_anim_init(&a);
    lv_anim_set_var(&a, img);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)voip_anim_fp_dialing_ring);
    lv_anim_set_path(&a, &path);
    lv_anim_set_values(&a, start, end);
    lv_anim_set_time(&a, 400);
    lv_anim_set_playback_time(&a, 400);
    lv_anim_set_playback_delay(&a, 0);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 500);

    lv_anim_start(&a);
}

static void voip_ring_animation(lv_obj_t * par)
{
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }

    lv_obj_t * img = lv_img_create(voip_phone_get_obj(), NULL);
    lv_img_set_src(img, LV_SYMBOL_LEFT);
    lv_obj_add_style(img, LV_OBJ_PART_MAIN, &lv_watch_image);
    lv_obj_set_style_local_text_font(img, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_y(img, lv_obj_get_y(par) + lv_obj_get_y(lv_obj_get_parent(par)));
    ext->phone_animation_obj = img;

    /*Create an animation to move the button continuously left to right*/
    lv_anim_t a;
    lv_anim_path_t path;
    lv_anim_value_t start = lv_obj_get_x(par) - 20;
    lv_anim_value_t end = start - 30;

    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_linear);

    lv_anim_init(&a);
    lv_anim_set_var(&a, img);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)voip_anim_fp_dialing_ring);
    lv_anim_set_path(&a, &path);
    lv_anim_set_values(&a, start, end);
    lv_anim_set_time(&a, 400);
    lv_anim_set_playback_time(&a, 400);
    lv_anim_set_playback_delay(&a, 0);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 500);

    lv_anim_start(&a);
}

static void voip_connected_animation(lv_obj_t * obj)
{
    lv_anim_t a;
    lv_anim_path_t path;

    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_step);

    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_start_cb(&a, (lv_anim_ready_cb_t)voip_anim_fp_connected);
    lv_anim_set_path(&a, &path);
    lv_anim_set_time(&a, 1);
    lv_anim_set_playback_time(&a, 0);
    lv_anim_set_playback_delay(&a, 0);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 1000);

    lv_anim_start(&a);
}

static void voip_anim_fp_connected(lv_anim_t * anim)
{
    lv_obj_t        *        obj = (lv_obj_t *)anim->var;
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }

    char str[20] = "";
    voip_get_time_elaps(ext->connect_start_time, str, 20);
    if(obj) {
        lv_label_set_text(obj, str);
    }
}

static void voip_phone_dialing(lv_obj_t * content)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }

    lv_obj_t * cont;

    // label dialing
    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(label, WATCH_TEXT_ID_DAILING);
    lv_obj_set_click(label, false);
    ext->phone_status_label = label;

    // img small
    lv_obj_t * img = lv_img_create(content, NULL);
    if(voipcall_params.img_src_small) {
        lv_img_set_src(img, voipcall_params.img_src_small);
    }

    cont = voip_phone_cont_create(content, LV_LAYOUT_ROW_MID, &lv_watch_style_transp, -1);

    // name
    label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    if(strlen(voipcall_params.name) > 0) {
        lv_label_set_text(label, voipcall_params.name);
    } else {
        lv_label_set_text(label, "");
    }
    lv_obj_set_click(label, false);

    // number
    label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text(label, voipcall_params.number);
    lv_obj_set_click(label, false);

    cont = voip_phone_cont_create(content, LV_LAYOUT_OFF, &lv_watch_style_transp, lv_obj_get_width(content) - 40);

    // btn volume
    lv_obj_t * btn;
    btn = lv_imgbtn_create(cont, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_VOLUME);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_VOLUME);
    lv_obj_set_event_cb(btn, voip_volume_btn_action);
    lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

    // btn ring off
    btn = lv_imgbtn_create(cont, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_OFF);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_OFF);
    lv_obj_set_event_cb(btn, voip_ring_off_action);
    lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
} /* voip_phone_dialing */

static void voip_phone_ring(lv_obj_t * content)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }
    lv_obj_t * label;

    // label ringing
    label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(label, WATCH_TEXT_ID_RINGING);
    lv_obj_set_click(label, false);
    ext->phone_status_label = label;

    // img small
    lv_obj_t * img = lv_img_create(content, NULL);
    if(voipcall_params.img_src_small) {
        lv_img_set_src(img, voipcall_params.img_src_small);
    }

    lv_obj_t * cont = voip_phone_cont_create(content, LV_LAYOUT_ROW_MID, &lv_watch_style_transp, -1);

    // name
    label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    if(strlen(voipcall_params.name) > 0) {
        lv_label_set_text(label, voipcall_params.name);
    } else {
        lv_label_set_text(label, "");
    }
    lv_obj_set_click(label, false);

    // number
    label = lv_label_create(cont, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text(label, voipcall_params.number);

    lv_obj_set_click(label, false);

    cont = voip_phone_cont_create(content, LV_LAYOUT_OFF, &lv_watch_style_transp, lv_obj_get_width(content) - 40);

    // btn ring off
    lv_obj_t * btn;
    btn = lv_imgbtn_create(cont, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_OFF);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_OFF);
    lv_obj_set_event_cb(btn, voip_ring_off_action);
    lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

    // btn ring hook
    btn = lv_imgbtn_create(cont, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_HOOK);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_HOOK);
    lv_obj_set_event_cb(btn, voip_ring_hook_action);
    lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
} /* voip_phone_ring */

static void voip_phone_connected(lv_obj_t * content)
{
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();

    LV_ASSERT_MEM(ext);
    if(ext == NULL) {
        return;
    }

    if(voipcall_params.voip_call_type == MODEM_VIDEO_CALL_TYPE_VOICE) {
        printf("voice call connected %s %d\n", __FUNCTION__, __LINE__);
        ext->local_remote_switch_flag = VOIP_REMOTE_SWITCH_NULL;
        // img small
        lv_obj_t * img = lv_img_create(content, NULL);

        printf("%s: img_src_small %p\n", __FUNCTION__, voipcall_params.img_src_small);
        if(voipcall_params.img_src_small) {
            lv_img_set_src(img, voipcall_params.img_src_small);
        }
        hal_rtc_t rtc_curr;                    // current rtc time
        Hal_Rtc_Gettime(&rtc_curr);
        ext->connect_start_time = time_to_seconds(&rtc_curr);

        lv_obj_t * cont = voip_phone_cont_create(content, LV_LAYOUT_ROW_MID, &lv_watch_style_transp, -1);
        lv_obj_set_width(cont, lv_obj_get_width(content));
        lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
        // name
        lv_obj_t * label = lv_label_create(cont, NULL);
        lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
        if(strlen(voipcall_params.name) > 0) {
            lv_label_set_text(label, voipcall_params.name);
        } else {
            lv_label_set_text(label, "");
        }
        lv_obj_set_click(label, false);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);

        // number
        label = lv_label_create(cont, NULL);
        lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_text(label, voipcall_params.number);
        lv_obj_set_click(label, false);

        // duration
        label = lv_label_create(content, NULL);
        lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_text(label, "0:0");
        lv_obj_set_click(label, false);
        lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
        ext->phone_status_label = label;

        cont = voip_phone_cont_create(content, LV_LAYOUT_OFF, &lv_watch_style_transp, lv_obj_get_width(content) - 40);

        // btn volume
        lv_obj_t * btn;
        btn = lv_imgbtn_create(cont, NULL);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_VOLUME);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_VOLUME);
        lv_obj_set_event_cb(btn, voip_volume_btn_action);
        lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

        // btn ring off
        btn = lv_imgbtn_create(cont, NULL);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_OFF);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_OFF);
        lv_obj_set_event_cb(btn, voip_ring_off_action);
        lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
    } else {
        lv_obj_t * canvas;
        lv_obj_t * label;
        lv_obj_t * btn;

        printf("video call connected %s %d\n", __FUNCTION__, __LINE__);

        ext->local_remote_switch_flag = VOIP_REMOTE_SWITCH_REMOTE;
        if((ext->voiph263fmt == VOIP_H263_FMT_SQCIF) || (ext->voiph263fmt == VOIP_H263_FMT_QCIF)) {
            hal_rtc_t rtc_curr;                        // current rtc time
            Hal_Rtc_Gettime(&rtc_curr);
            ext->connect_start_time = time_to_seconds(&rtc_curr);

            lv_obj_t * cont = voip_phone_cont_create(content, LV_LAYOUT_OFF, &lv_watch_style_transp, -1);
            lv_obj_set_width(cont, lv_obj_get_width(content));
            lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);

            /*Create the canvas object*/
            canvas = lv_canvas_create(cont, NULL);
            lv_obj_set_click(canvas, true);
            lv_obj_set_event_cb(canvas, canvas_button_hidden_event_cb);
            ext->canvas = canvas;

            lv_obj_align(canvas, cont, LV_ALIGN_IN_TOP_MID, 0, 0);
            /*Assign the buffer to the canvas*/
            lv_img_set_src(canvas, ICON_VIDEO_CALL);
            voip_signal_check_start(ext);

            // duration
            label = lv_label_create(canvas, NULL);
            lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
            lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
            lv_label_set_text(label, "0:0");
            lv_obj_set_click(label, false);
            lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
            if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(content)) {
                lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_RIGHT, -lv_obj_get_width(label), 0);
            }
            ext->phone_status_label = label;

            // signal weak notification
            label = lv_label_create(canvas, NULL);
            lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font30);
            lv_label_set_text_id(label, WATCH_TEXT_ID_SIGNAL_WEAK);
            lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
            lv_obj_align(label, canvas, LV_ALIGN_CENTER, 0, -10);
            lv_obj_set_click(label, false);
            lv_obj_set_hidden(label, true);
            ext->signal_weak_label = label;

            cont = voip_phone_cont_create(content, LV_LAYOUT_OFF, &lv_watch_style_transp, lv_obj_get_width(content) - 40);
            // btn volume
            btn = lv_imgbtn_create(cont, NULL);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_VOLUME);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_VOLUME);
            lv_obj_set_event_cb(btn, voip_volume_btn_action);
            lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

            // btn switch video
            btn = lv_imgbtn_create(cont, NULL);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_SETTING_VOLTE);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_SETTING_VOLTE);
            lv_obj_set_event_cb(btn, voip_local_remote_switch_action);
            lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_MID, 0, 0);

            // btn ring off
            btn = lv_imgbtn_create(cont, NULL);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_OFF);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_OFF);
            lv_obj_set_event_cb(btn, voip_ring_off_action);
            lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
        } else if((ext->voiph263fmt == VOIP_H263_FMT_CIF)
                  || (ext->voiph263fmt == VOIP_H263_FMT_4CIF)
                  || (ext->voiph263fmt == VOIP_H263_FMT_16CIF)
                  || (ext->voiph263fmt == VOIP_H263_FMT_OTHER)) {
            lv_obj_t * cont = lv_cont_create(content, NULL);
            lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES);
            lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_MID);
            /*Create the canvas object*/
            canvas = lv_canvas_create(cont, NULL);
            lv_obj_set_click(canvas, true);
            lv_obj_set_event_cb(canvas, canvas_button_hidden_event_cb);
            ext->canvas = canvas;

            lv_obj_t * btn_cont_par = canvas;

//#ifdef USE_WATCH_LITE
#if 0
            btn_cont_par = content;
            lv_obj_set_height(cont, LV_VER_RES - 100);
            lv_obj_set_style_local_bg_color(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
            lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
            lv_canvas_set_buffer(canvas, voip_phone_get_video_buffer(), VIDEO_SOURCE_WIDTH, VIDEO_SOURCE_HEIGHT, LV_IMG_CF_TRUE_COLOR);
           // lv_img_set_zoom(canvas, (LV_IMG_ZOOM_NONE * LV_HOR_RES) / VIDEO_SOURCE_WIDTH);
#else
            /*Assign the buffer to the canvas*/
            lv_img_set_src(canvas, ICON_VIDEO_CALL);
#endif
            voip_signal_check_start(ext);

            // duration
            label = lv_label_create(canvas, NULL);
            lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
            lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
            lv_label_set_text(label, "0:0");
            lv_obj_set_click(label, false);
            lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
            if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(content)) {
                lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_RIGHT, -lv_obj_get_width(label), 0);
            }
            ext->phone_status_label = label;
            ext->connect_time       = label;

            // signal weak notification
            label = lv_label_create(canvas, NULL);
            lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font30);
            lv_label_set_text_id(label, WATCH_TEXT_ID_SIGNAL_WEAK);
            lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
            lv_obj_align(label, canvas, LV_ALIGN_CENTER, 0, -10);
            lv_obj_set_click(label, false);
            lv_obj_set_hidden(label, true);
            ext->signal_weak_label = label;

            int8_t y_offset = -20;
            cont = lv_cont_create(btn_cont_par, NULL);
            lv_obj_set_size(cont, lv_obj_get_width(content) - 40, 70);
            lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
            lv_obj_set_click(cont, false);
            lv_obj_align(cont, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, y_offset);

            // btn volume
            btn = lv_imgbtn_create(cont, NULL);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_VOLUME);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_VOLUME);
            lv_obj_set_event_cb(btn, voip_volume_btn_action);
            lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
            lv_obj_add_style(btn, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
            ext->ring_value_button = btn;

            // btn switch video
            btn = lv_imgbtn_create(cont, NULL);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_SETTING_VOLTE);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_SETTING_VOLTE);
            lv_obj_set_event_cb(btn, voip_local_remote_switch_action);
            lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
            lv_obj_add_style(btn, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
            ext->view_switch_button = btn;

            // btn ring off
            btn = lv_imgbtn_create(cont, NULL);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_PHONE_RING_OFF);
            lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_PHONE_RING_OFF);
            lv_obj_set_event_cb(btn, voip_ring_off_action);
            lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
            lv_obj_add_style(btn, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
            ext->ring_off_button = btn;

            lv_obj_set_hidden(ext->connect_time, ext->canvas_button_hidden_flag);
            lv_obj_set_hidden(ext->ring_value_button, ext->canvas_button_hidden_flag);
            lv_obj_set_hidden(ext->view_switch_button, ext->canvas_button_hidden_flag);
            lv_obj_set_hidden(ext->ring_off_button, ext->canvas_button_hidden_flag);
        }

        if(voip_phone_get_obj()) {
            lv_watch_obj_set_down_slide_cb(voip_phone_get_obj(), voip_phone_down_slide_callback);
        }
    }
} /* voip_phone_connected */

static void voip_phone_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    lv_watch_png_cache_all_free();

    // stop playing tone
    Hal_Tone_Play_End();

    // ring off
    voip_ring_off_action(NULL, LV_EVENT_CLICKED);

    // stop ring off timer
    if(NULL != voipcall_params.ring_off_timer_id) {
        Hal_Timer_Stop(voipcall_params.ring_off_timer_id);
        voipcall_params.ring_off_timer_id = NULL;
    }

    // stop callreq timer
    if(NULL != voipcall_params.callreq_timer_id) {
        Hal_Timer_Stop(voipcall_params.callreq_timer_id);
        voipcall_params.callreq_timer_id = NULL;
    }

    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
    voipcall_params.state = MMI_VOIP_CALL_STATE_NULL;
#ifdef USE_WATCH_LITE
    lv_mem_aligned_free(g_trans_buf);
    g_trans_buf = NULL;
#endif
}

static void voip_get_time_elaps(uint32_t last_time, char * str, int8_t size)
{
    hal_rtc_t rtc_curr;            // current rtc time
    uint32_t  second_curr = 0;     // seconds of current time

    Hal_Rtc_Gettime(&rtc_curr);
    second_curr = time_to_seconds(&rtc_curr);

    uint32_t elaps   = second_curr - last_time;
    uint32_t seconds = elaps % 60;
    uint32_t minutes = elaps / 60;
    uint32_t hours   = minutes / 60;
    minutes = minutes % 60;

    if(hours == 0) {
        if(lv_lang_act() == LANG_CH) {
            snprintf(str, size, "%.2d:%.2d", minutes, seconds);
        } else {
            snprintf(str, size, "%.2d:%.2d", minutes, seconds);
        }
    } else {
        if(lv_lang_act() == LANG_CH) {
            snprintf(str, size, "%.2d:%.2d:%.2d", hours, minutes, seconds);
        } else {
            snprintf(str, size, "%.2d:%.2d:%.2d", hours, minutes, seconds);
        }
    }
}

#if VOIP_PHONE_TEST != 0

static void watch_voip_call_response_originating(int32_t index)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    if(MMI_VOIP_CALL_STATE_DIALING == voipcall_params.state) {
        return;
    }

    printf("%s: %d\n", __func__, index);
    voipcall_params.index = index;

    // state: NULL -> DIALLING
    voipcall_params.state = MMI_VOIP_CALL_STATE_DIALING;
}
#endif

void watch_voip_call_response_alerting(int32_t index)
{
    printf("%s %d index is %d\n", __FUNCTION__, __LINE__, index);
    if(MMI_VOIP_CALL_STATE_ALERTING == voipcall_params.state) {
        return;
    }

    uint8_t volume;

    // state: DIALLING -> ALERTING
    voipcall_params.state = MMI_VOIP_CALL_STATE_ALERTING;

    // set speaker volume
    UI_NV_Read_Req(NV_SECTION_UI_PHONE, VOIP_PHONE_NV_VOLUME_OFFSET, 1, &volume);
    if(!Hal_Set_BT_Audio_Path((HAL_SPEAKER_GAIN)volume))
        Hal_Call_Speaker(HAL_CALL_SPK_ON, (HAL_SPEAKER_GAIN)volume);
}

void watch_voip_call_response_ring(int32_t index)
{
    printf("%s: %d\n", __func__, index);
    Hal_NFFS_File_Play_Repeat(AUDIO_INCOMING_CALL, query_current_volume());

    voipcall_params.state = MMI_VOIP_CALL_STATE_ALERTING;
}

void watch_voip_call_response_connect(int32_t index)
{
    printf("%s %d index is %d\n", __FUNCTION__, __LINE__, index);
    if(MMI_VOIP_CALL_STATE_ACTIVE == voipcall_params.state) {
        return;
    }

    voipcall_params.state = MMI_VOIP_CALL_STATE_ACTIVE;

    // stop callreq timer
    if(NULL != voipcall_params.callreq_timer_id) {
        Hal_Timer_Stop(voipcall_params.callreq_timer_id);
        voipcall_params.callreq_timer_id = NULL;
    }

    // stop playing tone
    Hal_Tone_Play_End();

    Hal_Set_Voice_Call_Status(VOICE_CALL_START);

    // create connected UI
    voip_phone_create(NULL, VOIP_PHONE_UI_CONNECTED);

    // set speaker volume
    uint8_t volume;
    UI_NV_Read_Req(NV_SECTION_UI_PHONE, VOIP_PHONE_NV_VOLUME_OFFSET, 1, &volume);
    if(!Hal_Set_BT_Audio_Path((HAL_SPEAKER_GAIN)volume)) {
        printf("%s: not set bt audio path, volume %d\n", __FUNCTION__, volume);
        Hal_Call_Speaker(HAL_CALL_SPK_ON, (HAL_SPEAKER_GAIN)volume);
    }
}

void watch_voip_call_response_disconnect(int32_t index)
{
    // not used
    (void)index;
    printf("%s: %d\n", __func__, index);
    Hal_Tone_Play_End();

    // create ring off UI
    if(voip_phone_get_activity_obj()) {
        voip_phone_ring_off_create();
        // remove donw slider handle
        lv_watch_obj_set_down_slide_cb(voip_phone_get_obj(), NULL);
    }
    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
    voipcall_params.state = MMI_VOIP_CALL_STATE_NULL;
}

void watch_voip_call_response_no_carrier(int32_t index, uint8_t isNoCarrier)
{
    printf("%s: %d\n", __func__, __LINE__);

    if((false == voipcall_params.ring_off_timeout)
            && (1 == isNoCarrier)
            && (MMI_VOIP_CALL_STATE_NULL == voipcall_params.state)) {
        printf("%s: ignore no carrier in state NULL\n", __func__);
        return;
    }

    printf("%s: %d\n", __func__, index);

    // stop playing tone
    Hal_Tone_Play_End();

    // stop ring off timer
    if(NULL != voipcall_params.ring_off_timer_id) {
        Hal_Timer_Stop(voipcall_params.ring_off_timer_id);
        voipcall_params.ring_off_timer_id = NULL;
    }

    // stop callreq timer
    if(NULL != voipcall_params.callreq_timer_id) {
        Hal_Timer_Stop(voipcall_params.callreq_timer_id);
        voipcall_params.callreq_timer_id = NULL;
    }

    // create ring off UI
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);
    if(activity_obj) {
        voip_phone_ring_off_create();
    }
#if 0
    // display alarm if it is buffered
    if(voipcall_params.buffer_alarm) {
        Hal_NFFS_File_Play_Repeat(AUDIO_ALARM, query_current_volume());
        alarm_create(NULL);
    } else {
        // indicate voice msg
        voice_msg_call_end();
    }
#endif
    bool switch_path = false;
    if(MMI_MODEM_CALL_STATE_NULL == phone_get_call_state()) {
        Hal_Set_Voice_Call_Status(VOICE_CALL_END);
        switch_path = Hal_Disconnect_BT_Audio_Path();
    } else {
        printf("%s tele call is on going, keep para of audio\n", __FUNCTION__);
    }
    if(switch_path) {
#if USE_LV_BLUETOOTH
        watch_bt_sco_set_call_type(1);
#endif
    } else{
        Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_2);
    }
    watch_wakeup_time_reset();

    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
    voipcall_params.state = MMI_VOIP_CALL_STATE_NULL;
} /* watch_voip_call_response_no_carrier */

void watch_voip_call_response_state_change(MMI_Modem_Call_Response_State_Change_t * state)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    for(uint8_t i = 0; i < state->count; i++) {
        Hal_Mem_Free(state->calls[i].number);
        Hal_Mem_Free(state->calls[i].name);
    }
    Hal_Mem_Free(state->calls);
    Hal_Mem_Free(state);
}

void voip_phone_voice_call_req(char * number, MODEM_VIDEO_CALL_TYPE voip_call_type)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);

    char * dial_number = NULL;

    // check call state
    if(MMI_VOIP_CALL_STATE_NULL != voipcall_params.state) {
        printf("%s: only support one call, state=%d\n", __func__, voipcall_params.state);
        Hal_Mem_Free(number);
        return;
    }

    // stop ring off timer
    if(NULL != call_params.ring_off_timer_id) {
        Hal_Timer_Stop(call_params.ring_off_timer_id);
        call_params.ring_off_timer_id = NULL;
    }

    // stop callreq timer
    if(NULL != voipcall_params.callreq_timer_id) {
        Hal_Timer_Stop(voipcall_params.callreq_timer_id);
        voipcall_params.callreq_timer_id = NULL;
    }

    // set call parameters
    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
    voipcall_params.is_mt          = false;
    voipcall_params.state          = MMI_VOIP_CALL_STATE_DIALING;
    voipcall_params.voip_call_type = voip_call_type;

    strncpy(voipcall_params.number, number, NV_CONTACTS_MAX_NUMBER_LEN);

    voip_phonebook_contact_t * contact_info = voip_phonebook_get_contact_info(number);
    if(NULL != contact_info) {
        Hal_Mem_Copy(voipcall_params.name, contact_info->contact_name, NV_CONTACTS_MAX_NAME_LEN);
        voipcall_params.img_src       = contact_info->img_src;
        voipcall_params.img_src_small = contact_info->img_src_small;

        Hal_Mem_Free(contact_info);
    } else {
        voipcall_params.img_src       = ICON_FAMILY;
        voipcall_params.img_src_small = ICON_FAMILY_SMALL;
    }

    // Initiate voice call
    dial_number = (char *)Hal_Mem_Alloc(strlen(voipcall_params.number) + 1);
    strncpy(dial_number, voipcall_params.number, strlen(voipcall_params.number) + 1);
    printf("%s %d\n", __FUNCTION__, __LINE__);
    int8_t ret = voip_call_dial_req(voip_call_type, dial_number);
    UINT32 timeout;
    if(NULL == voipcall_params.callreq_timer_id) {
        if(ret >= 0) {
            timeout = VOIP_CALLREQ_TIMER_LEN;
        } else {
            timeout = 1000; //ms
        }
        voipcall_params.callreq_timer_id = Hal_Timer_Start(timeout, voip_phone_callreq_timeout_cb, NULL, 0);
    }
    // create dialing UI
    voip_rat_check_start();
    voip_phone_create(NULL, VOIP_PHONE_UI_DIALING);
    Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_2,voip_audio_ctrl_callback);
    Hal_Set_Voice_Call_Status(VOICE_CALL_START);
} /* voip_phone_voice_call_req */

void watch_voip_call_response_bottom_error(void)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    voip_phone_cleanup();
    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
}

void watch_voip_call_response_bottom_need_redail(void)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    //voip_call_dial_req(voipcall_params.voip_call_type, voipcall_params.number);
}

int get_voipcall_params_index()
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    return(voipcall_params.index);
}

void watch_voip_incomingcall_response(char * number, int32_t calltype)
{
    char           *          pNumber = NULL;
    uint8_t                  volume;
    voip_phonebook_contact_t * contact_info = NULL;

    printf("%s: number=%s, calltype %d\n", __FUNCTION__, number, calltype);

    if((MMI_MODEM_CALL_STATE_NULL != phone_get_call_state()) || is_rat_gsm()) {
        printf("tele call is on going  %d or in gsm mode, reject voip call\n", phone_get_call_state());
        voip_call_reject_req(TRUE);
        return;
    }

    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
    voipcall_params.voip_call_type = (MODEM_VIDEO_CALL_TYPE)calltype;
//    voipcall_params.index          = index;
    voipcall_params.is_mt          = true;
    voipcall_params.state          = MMI_VOIP_CALL_STATE_INCOMING;
    strncpy(voipcall_params.number, number, NV_CONTACTS_MAX_NUMBER_LEN);

    pNumber = voip_phonebook_get_phone_number(0);
    if(NULL == pNumber) {     // no contact
        contact_info = (voip_phonebook_contact_t *)Hal_Mem_Alloc(sizeof(voip_phonebook_contact_t));
        Hal_Mem_Set(contact_info, 0, sizeof(voip_phonebook_contact_t));
        contact_info->img_src       = ICON_FAMILY;
        contact_info->img_src_small = ICON_FAMILY_SMALL;
        printf("%s: img_src_small %p\n", __FUNCTION__, contact_info->img_src_small);
        contact_info->contact_type = 2;
        Hal_Mem_Free(number);
    } else {
        Hal_Mem_Free(pNumber);
        contact_info = voip_phonebook_get_contact_info(number);
    }

    if(NULL != contact_info) {
        Hal_Mem_Copy(voipcall_params.name, contact_info->contact_name, NV_CONTACTS_MAX_NAME_LEN);
        voipcall_params.img_src       = contact_info->img_src;
        voipcall_params.img_src_small = contact_info->img_src_small;
        Hal_Mem_Free(contact_info);

#if 0
        // clean up alarm if any
        alarm_cleanup();

        // inform speech recog & voice msg
        speech_recog_mt_call_ind();
        voice_msg_mt_call_ind();
#endif
        if(AUDIO_CTRL_PRIORITY_2 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_2,voip_audio_ctrl_callback))
        {
            watch_set_lcd_status(true);
            // set speaker volume

            UI_NV_Read_Req(NV_SECTION_UI_PHONE, VOIP_PHONE_NV_VOLUME_OFFSET, 1, &volume);
            if(!Hal_Set_BT_Audio_Path((HAL_SPEAKER_GAIN)volume))
                Hal_Call_Speaker(HAL_CALL_SPK_ON, (HAL_SPEAKER_GAIN)volume);

            // play tone
            Hal_NFFS_File_Play_Repeat(AUDIO_INCOMING_CALL, query_current_volume());

            // start rat checking
            voip_rat_check_start();

            // create ring UI
            voip_phone_create(NULL, VOIP_PHONE_UI_INCOMING);
        }
        else
        { // AUDIO_CTRL_PRIORITY_1
            printf("%s: phone call has been run, auto reject\n", __func__);
            // auto reject
            voip_call_reject_req(FALSE);
            voip_signal_check_stop();
            voip_rat_check_stop();

            // stop ring off timer
            if(NULL != voipcall_params.ring_off_timer_id) {
                Hal_Timer_Stop(voipcall_params.ring_off_timer_id);
                voipcall_params.ring_off_timer_id = NULL;
            }

            // stop callreq timer
            if(NULL != voipcall_params.callreq_timer_id) {
                Hal_Timer_Stop(voipcall_params.callreq_timer_id);
                voipcall_params.callreq_timer_id = NULL;
            }

            Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
            voipcall_params.state = MMI_VOIP_CALL_STATE_NULL;
            voip_set_volume_when_slider_act();
            voip_phone_cleanup();
            lv_watch_png_cache_all_free();
            Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_2);
        }
    } else {
        printf("%s: not a contact, auto reject\n", __func__);
        // auto reject
        voip_call_reject_req(TRUE);

#if VOIP_PHONE_TEST
        watch_voip_call_response_no_carrier(voipcall_params.index, 0);
#endif
    }
} /* watch_voip_incomingcall_response */

static lv_res_t voip_call_fail_ind(lv_obj_t * obj)
{
    printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_obj_t * call_fail_obj = lv_obj_create(obj, NULL);
    LV_ASSERT_MEM(call_fail_obj);
    if(call_fail_obj == NULL) {
        return(LV_RES_INV);
    }

    lv_obj_set_size(call_fail_obj, lv_obj_get_width(obj), lv_obj_get_height(obj));
    lv_obj_add_style(call_fail_obj, LV_OBJ_PART_MAIN, &lv_style_transp_tight);
    lv_obj_t * content = lv_cont_create(call_fail_obj, NULL);
    LV_ASSERT_MEM(content);

    if(content == NULL) {
        return(LV_RES_INV);
    }
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_cont_opa1);
    lv_obj_set_size(content, 204, 44);
    lv_obj_align(content, call_fail_obj, LV_ALIGN_CENTER, 0, 30);
    lv_cont_set_layout(content, LV_LAYOUT_CENTER);
    lv_obj_set_click(content, false);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);

    if(is_rat_gsm())
        lv_label_set_text_id(label, WATCH_TEXT_ID_NETWORK_WRONG);
    else
        lv_label_set_text_id(label, WATCH_TEXT_ID_NO_SIM_OR_NETWORK);
    lv_obj_set_click(label, false);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, call_fail_obj);
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)voip_call_fail_message_cleanup);
    lv_anim_set_time(&a, 2000);
    lv_anim_start(&a);

    return(LV_RES_OK);
}

static void voip_call_fail_message_cleanup(lv_anim_t * anim)
{
    lv_obj_del(anim->var);
}

void watch_ui_voip_message_handle(void * msg)
{
}

void watch_voip_autotest_handle(void * testmsg)
{
}

static bool voip_check_mo_permission(void)
{
    bool is_allowed = true;
    if((!watch_modem_sim_present_check_req())
       || (MMI_MODEM_SIGNAL_BAR_0 == watch_modem_get_signal_bar_req())
//       || (MODEM_VIDEO_CALL_REG_OK != watch_modem_video_call_get_reg_stat_req())
       || (is_rat_gsm())) {
        is_allowed = false;
    }
#if USE_LV_WLAN != 0
    if(false == is_allowed) {
        if(hal_wlan_is_connected_state()) {
            is_allowed = true;
        }
    }
#endif
    return(is_allowed);
}

static void voip_local_remote_switch_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    printf("%s %d\n", __FUNCTION__, __LINE__);
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOIPPHONE);
    if(!activity_obj) {
        return;
    }
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();
    if(!ext) {
        return;
    }
    if(ext->local_remote_switch_flag == VOIP_REMOTE_SWITCH_NULL) {
        return;
    }
    if(ext->local_remote_switch_flag == VOIP_REMOTE_SWITCH_REMOTE) {
        ext->local_remote_switch_flag = VOIP_REMOTE_SWITCH_LOCAL;
    } else {
        ext->local_remote_switch_flag = VOIP_REMOTE_SWITCH_REMOTE;
    }
}

static void canvas_button_hidden_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        printf("%s %d\n", __FUNCTION__, __LINE__);
        lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();
        if(!ext) {
            return;
        }
        if(ext->canvas_button_hidden_flag == NOT_HIDDEN) {
            ext->canvas_button_hidden_flag = HIDDEN;
        } else {
            ext->canvas_button_hidden_flag = NOT_HIDDEN;
        }

        lv_obj_set_hidden(ext->connect_time, ext->canvas_button_hidden_flag);
        lv_obj_set_hidden(ext->ring_value_button, ext->canvas_button_hidden_flag);
        lv_obj_set_hidden(ext->view_switch_button, ext->canvas_button_hidden_flag);
        lv_obj_set_hidden(ext->ring_off_button, ext->canvas_button_hidden_flag);
    }
}

static void voip_set_volume_when_slider_act(void)
{
    printf("%s\n", __FUNCTION__);
    if(lv_watch_get_activity_obj(ACT_ID_SLIDER)) {
        if(MMI_VOIP_CALL_STATE_NULL != voipcall_params.state) {
            uint8_t volume = voip_get_call_volume();
            printf("set nv, speaker volume is %d.\n", volume);
            UI_NV_Write_Req(NV_SECTION_UI_PHONE, 0, 1, &volume);
        }
    }
}

#if VIDEO_SIGNAL_CHECK
static void signal_check(lv_task_t *task)
{
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();
    if(!ext) return;

    if((false == is_rat_gsm()) &&
       (watch_modem_get_signal_bar_req() < MMI_MODEM_SIGNAL_BAR_3)) {
        lv_obj_set_hidden(ext->signal_weak_label, false);
    } else {
        lv_obj_set_hidden(ext->signal_weak_label, true);
    }
}
#endif

static void voip_signal_check_start(void * ext)
{
#if VIDEO_SIGNAL_CHECK
    if(NULL == signal_check_task)
        signal_check_task = lv_task_create(signal_check, VIDEO_SIGNAL_CHECK_PERIOD, LV_TASK_PRIO_HIGH, NULL);
#endif
}

static void voip_signal_check_stop(void)
{
#if VIDEO_SIGNAL_CHECK
    if(signal_check_task) {
        lv_task_del(signal_check_task);
        signal_check_task = NULL;
    }
#endif
}

static bool is_rat_gsm(void)
{
    MMI_MODEM_PLMN_RAT rat;

    watch_modem_get_operator_req(&rat);
    if(MMI_MODEM_PLMN_RAT_GSM == rat)
        return true;
    else
        return false;
}

static void rat_check(lv_task_t *task)
{
    if(is_rat_gsm()) {
        voip_ring_off_action(NULL, LV_EVENT_CLICKED);
    }
}

static void voip_rat_check_start(void)
{
    if(NULL == rat_check_task)
        rat_check_task = lv_task_create(rat_check, VIDEO_RAT_CHECK_PERIOD, LV_TASK_PRIO_HIGH, NULL);
}

static void voip_rat_check_stop(void)
{
    if(rat_check_task) {
        lv_task_del(rat_check_task);
        rat_check_task = NULL;
    }
}

void voip_fps_check(int8_t fps)
{
    lv_voip_phone_obj_ext_t * ext = voip_phone_get_ext();
    if(NULL != ext) {
        if((VIDEO_CALL_FPS_CHECK_LIMIT >= fps) && (0 != fps)) {
            printf("%s %d\n", __FUNCTION__, fps);
            if(lv_obj_get_hidden(ext->signal_weak_label) == false) return;
            lv_obj_set_hidden(ext->signal_weak_label, false);
        } else {
            if(lv_obj_get_hidden(ext->signal_weak_label) == true) return;
            lv_obj_set_hidden(ext->signal_weak_label, true);
        }
    }
}

#endif // #if USE_LV_WATCH_VIDEOCALL != 0

//interface
uint8_t voip_get_call_volume(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    return(voipcall_params.speaker_volume);
#else
    return 0;
#endif
}

bool watch_voip_suspend_check(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    if(voipcall_params.voip_call_type == MODEM_VIDEO_CALL_TYPE_VIDEO) {
        return(false);
    }

    if((MMI_VOIP_CALL_STATE_INCOMING == voipcall_params.state)
            || (MMI_VOIP_CALL_STATE_DIALING == voipcall_params.state)) {
        return(false);
    }
#endif
    return(true);
}

#if 0
void voip_alarm_ind(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    voipcall_params.buffer_alarm = true;
#endif
}
#endif

bool voip_connect_check_req(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    if(voip_phone_get_activity_obj()) {
        return(true);
    }
#endif
    return(false);
}

void voip_ring_off_req(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    voip_ring_off_action(NULL, LV_EVENT_CLICKED);
#endif
}

void voip_activity_destroy_req(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    printf("%s\n", __FUNCTION__);

    lv_obj_t * activity_obj = voip_phone_get_activity_obj();

    if(!activity_obj) {
        return;
    }

    switch(voipcall_params.state) {
        case MMI_VOIP_CALL_STATE_NULL:
        case MMI_VOIP_CALL_STATE_INVALID:
            // do noting
            break;

        case MMI_VOIP_CALL_STATE_ACTIVE:
            voip_call_hangup_req(FALSE);
            break;

        case MMI_VOIP_CALL_STATE_INCOMING:
            Hal_Tone_Play_End();
            voip_call_reject_req(FALSE);
            break;

        default:
            voip_call_hangup_req(FALSE);
            break;
    } /* switch */

    voip_signal_check_stop();
    voip_rat_check_stop();

    // stop ring off timer
    if(NULL != voipcall_params.ring_off_timer_id) {
        Hal_Timer_Stop(voipcall_params.ring_off_timer_id);
        voipcall_params.ring_off_timer_id = NULL;
    }

    // stop callreq timer
    if(NULL != voipcall_params.callreq_timer_id) {
        Hal_Timer_Stop(voipcall_params.callreq_timer_id);
        voipcall_params.callreq_timer_id = NULL;
    }

    Hal_Mem_Set(&voipcall_params, 0, sizeof(voip_phone_call_params_t));
    voipcall_params.state = MMI_VOIP_CALL_STATE_NULL;
    voip_set_volume_when_slider_act();
    voip_phone_cleanup();
    lv_watch_png_cache_all_free();
#endif
} /* voip_activity_destroy_req */

void voip_bt_connected(void)
{
#if USE_LV_WATCH_VIDEOCALL != 0
    if((MMI_VOIP_CALL_STATE_ALERTING == voipcall_params.state) ||
       (MMI_VOIP_CALL_STATE_ACTIVE == voipcall_params.state)) {
        Hal_Set_BT_Audio_Path((HAL_SPEAKER_GAIN)voip_get_call_volume());
    }
#endif
}
